import { list } from '@/api/monitor/logininfor';
<!--
 * @Description: 
 * @Date: 2023-12-20 16:39:40
 * @LastEditTime: 2024-02-02 08:50:14
-->
<template>
  <div class="dbxxgroup">
    <!-- <el-alert
      show-icon
      title="拖动卡片可以调整顺序！"
      type="error"
      effect="dark"
    >
    </el-alert> -->
    <div class="dbxxgroup-list">
      <!-- <draggable
        v-model="groupList"
        class="dragArea"
        ghostClass="ghost"
        chosenClass="chosen"
        filter=".forbid"
        @update="updateFn"
      >
        <transition-group> -->
      <el-row :gutter="30">
        <el-col
          :xs="24"
          :sm="24"
          :md="8"
          :lg="6"
          :xl="4"
          v-for="item in groupList"
          :class="item.name == 'add' ? 'item forbid' : 'item'"
          :key="item.deId"
          style="margin-bottom: 20px"
        >
          <div class="menu-item animate__animated animate__zoomIn">
            <template v-if="item.name != 'add'">
              <div>
                <img src="../../../assets/images/guohui.png" />
                <span>{{ item.name }}</span>
              </div>
              <div>
                <span @click.stop="handleUpdate(item)">修改</span>
                <span @click.stop="handleDelete(item)">删除</span>
              </div>
            </template>
            <template v-else>
              <div @click.stop="handleAdd">
                <i class="el-icon-plus" style="font-size: 50px"></i>
              </div>
            </template>
          </div>
        </el-col>
      </el-row>

      <!-- </transition-group>
      </draggable> -->
    </div>
    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入代表团名称"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="显示排序" prop="orderId">
              <el-input-number
                v-model="form.orderId"
                controls-position="right"
                :min="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import draggable from "vuedraggable";
import _ from "lodash";
import {
  addDelegation,
  getDelegationItem,
  addDelegationList,
  updateDelegationList,
  deleteDelegation,
} from "@/api/dbxx/dbxxgroup/index";

export default {
  name: "Group",
  components: {
    draggable,
  },

  data() {
    return {
      groupList: [],
      form: {},
      rules: {
        name: [
          { required: true, message: "请输入代表团名称", trigger: "blur" },
          {
            min: 2,
            max: 30,
            message: "长度在 2 到 30 个字符",
            trigger: "blur",
          },
        ],
        orderId: [
          { required: true, message: "请输入显示排序", trigger: "blur" },
        ],
      },
      open: false,
      title: "添加用户配置",
    };
  },
  created() {},
  mounted() {
    this.getList();
  },
  computed: {
    updateFn() {
      // 防抖
      return _.debounce(this.handleUpdateDrag, 1000);
    },
  },
  methods: {
    getList() {
      addDelegationList({ orderByColumn: "orderId", isAsc: "asc" }).then(
        (res) => {
          this.groupList = res.rows;
          this.groupList.push({ name: "add", deId: "add" });
        }
      );
    },
    reset() {
      this.form = {
        name: undefined,
        orderId: 0,
        remark: undefined,
      };
      this.resetForm("form");
    },
    handleUpdate({ deId }) {
      this.reset();
      getDelegationItem(deId).then((res) => {
        this.form = res.data;
        this.title = "修改代表团队信息";
        this.open = true;
      });
    },
    handleDelete({ deId, name }) {
      this.$modal
        .confirm('是否确认删除字典编号为"' + name + '"的数据项？')
        .then(function () {
          return deleteDelegation(deId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleAdd() {
      this.reset();
      this.title = "添加代表团队";
      this.open = true;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.deId != undefined) {
            updateDelegationList(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDelegation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        } else {
          return false;
        }
      });
    },
    handleUpdateDrag(evt) {
      let event = this.groupList[evt.newIndex];
      let event2 = this.groupList[evt.oldIndex];
      let paramsArr = [
        updateDelegationList({ ...event, orderId: evt.newIndex }),
        updateDelegationList({ ...event2, orderId: evt.oldIndex }),
      ];
      Promise.all(paramsArr)
        .then((posts) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        })
        .catch(function (reason) {
          console.log("出错了", reason);
        });
    },
    cancel() {
      this.open = false;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/mixin.scss";
.dbxxgroup {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overflow: auto;
  padding: 10px 30px;
  @include flex(y);
  &-list {
    width: 100%;
    margin-top: 20px;
    .menu-item {
      width: 100%;
      height: 160px;
      background-color: transparent;
      border-radius: 5px;
      @include flex(y, null, center);
      border: 1px dashed #ccc;
      position: relative;
      & > div:nth-child(1) {
        width: 100%;
        flex: 1;
        @include flex(y, center, center);
        & > span {
          width: 100%;
          text-align: center;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          color: red;
          font-size: 24px;
          font-weight: bold;
          font-family: "隶书", "楷体";
        }
        & > img {
          width: 60px;
          height: 60px;
          margin-bottom: 10px;
          object-fit: cover;
        }
      }
      & > div:nth-child(2) {
        width: 100%;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        position: absolute;
        top: 10px;
        right: 5px;
        & > span {
          height: 25px;
          line-height: 25px;
          padding: 0 5px;
          text-align: center;
          color: #fff;
          cursor: pointer;
          font-size: 13px;
        }
        & > span:nth-of-type(1) {
          background: #ffb800;
          margin-right: 5px;
        }
        & > span:nth-of-type(2) {
          background: #ff5722;
        }
      }
    }
    .menu-item:hover {
      border: 1px dashed #ff5722;
      background-color: #fff;
    }
  }
}
.dragArea {
  & > span {
  }
}
.chosen {
  // background-color: rgba(0, 0, 0, 0.5) !important;
  // color: #fff;
}
.ghost {
  background-color: #c11c06 !important;
  color: #fff;
}
</style>
